from django.db import models
from itsdangerous import BadData
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from django.conf import settings

class BaseModel(models.Model):
    create_time = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')
    update_time = models.DateTimeField(auto_now=True, verbose_name='跟新时间')

    class Meta:
        abstract = True




# ==========================================加密========================================

def dumps_id_way(openid):
    # 生成对象
    serializer = Serializer(settings.SECRET_KEY, expires_in=3600)
    data = {'openid': openid}
    token = serializer.dumps(data)
    return token.decode()


def loads_id_way(openid_b):
    serializer = Serializer(settings.SECRET_KEY, expires_in=3600)
    try:
        openid_dict = serializer.loads(openid_b)
    except BadData:
        print('解密失败')
    return openid_dict.get('openid')
